package com.fuershiye.woman.monitor.utils;

import com.fuershiye.woman.monitor.constants.CodeName;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.*;

@SuppressWarnings("unchecked")
public class SortUtil {


	// 固定部门排序
	private static final String DEPART_SORT= "山东省,济南市,青岛市,淄博市,枣庄市,东营市,烟台市,潍坊市,济宁市,泰安市,威海市,日照市,滨州市,德州市,聊城市,临沂市,菏泽市";

	public static List<Map.Entry<String,String>> sortMap(Map<String, String> map) {

		List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(map.entrySet());
		Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
			//升序排序
			public int compare(Map.Entry<String, String> o1,
							   Map.Entry<String, String> o2) {
				return o1.getValue().compareTo(o2.getValue());
			}

		});

		return list;
	}

	//对list进行重新按照code进行升序-从小到大
	public static List<Map<String,Object>> sortListByCode(List<Map<String, Object>> list) {

		if (null != list&& list.size()>0) {

			Collections.sort(list,new Comparator<Map>() {
				@Override
				public int compare(Map o1, Map o2) {
					int ret = 0;
					//比较两个对象的顺序，如果前者小于、等于或者大于后者，则分别返回-1/0/1
					ret = o1.get(CodeName.CODE).toString().compareTo(o2.get(CodeName.CODE).toString());//逆序的话就用o2.compareTo(o1)即可
					return ret;
				}
			});
		}

		return list;
	}

	//对list进行重新按照部门 code进行升序-从小到大
	public static List<Map<String,Object>> sortListByAreaCode(List<Map<String, Object>> list) {

		if (null != list&& list.size()>0) {

			Collections.sort(list,new Comparator<Map>() {
				@Override
				public int compare(Map o1, Map o2) {
					int ret = 0;
					//比较两个对象的顺序，如果前者小于、等于或者大于后者，则分别返回-1/0/1
					ret = o1.get(CodeName.CODE).toString().compareTo(o2.get(CodeName.CODE).toString());//逆序的话就用o2.compareTo(o1)即可
					return ret;
				}
			});
		}
		return list;
	}

	/**
	 * 对list按照部门固定顺序排序 配置文件中的  departSort
	 //	{
	 //		"sname": "济南市",
	 //			"2014": "1_0_93.22_0_",
	 //			"departid": 32
	 //	},
	 //	{
	 //		"sname": "东营市",
	 //			"2014": "1_0_46_0_",
	 //			"departid": 47
	 //	},
	 //	{
	 //		"sname": "德州市",
	 //			"2014": "1_0_54.3_0_",
	 //			"departid": 33
	 //	}
	 * @param list
	 * @return
	 */
	public static List<Map<String,Object>> sortListByDepartPreset(List<Map<String, Object>> list) {

		List<Map<String, Object>> result = Lists.newArrayList();

		// list 变成 Map 方便按key取值
		Map<String, Object> mapNameMaps = Maps.newConcurrentMap();
		for (Map<String, Object> map : list) {
			mapNameMaps.put(map.get("sname").toString(), map);
		}

		List<String> departlist = Arrays.asList(DEPART_SORT.split(","));
		for (String depart : departlist) {
			if(mapNameMaps.containsKey(depart)){
				result.add((Map)mapNameMaps.get(depart));
				mapNameMaps.remove(depart);
			}
		}

		// 未命中的数据，追加到最后
		for (Map.Entry<String, Object> map : mapNameMaps.entrySet()){
			result.add((Map)map.getValue());
		}

		return result;
	}



	public static void main(String[] args) throws Exception {
//
//		Map<String, String> mapSort = new HashMap<String, String>();
//		mapSort.put("code", "0");
//		mapSort.put(CodeName.AREA_CODE, "ccccc");
//		mapSort.put("mapa", "aaaaa");
//		mapSort.put("mapb", "bbbbb");
//		mapSort.put("mapd", "ddddd");
//
//		List<Map<String, Object>> list = Lists.newArrayList();
//		Map<String, Object> map1 = new HashMap<String, Object>();
//		map1.put("code", "1");
//		map1.put(CodeName.AREA_CODE, "");
//		map1.put("map1a", "aaaaa");
//		map1.put("map1b", "bbbbb");
//		map1.put("map1d", "ddddd");
//		list.add(map1);
//
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("code", "0");
//		map.put(CodeName.AREA_CODE, "");
//		map.put("mapa", "aaaaa");
//		map.put("mapb", "bbbbb");
//		map.put("mapd", "ddddd");
//		list.add(map);
//
//		Map<String, Object> map2 = new HashMap<String, Object>();
//		map2.put("code", "2");
//		map2.put(CodeName.AREA_CODE, "");
//		map2.put("map2a", "aaaaa");
//		map2.put("map2b", "bbbbb");
//		map2.put("map2d", "ddddd");
//		list.add(map2);
//
////		//把某个map，按照key排序
////		List<Map.Entry<String,String>> mapList = sortMap(mapSort);
////		for(Map.Entry<String,String> mapping:mapList){
////			System.out.println(mapping.getKey()+":"+mapping.getValue());
////		}
//
////		for(Map<String,Object> mapping:list){
////			System.out.println(mapping.get("code"));
////		}
//
//		list = sortListByAreaCode(list);
//		for(Map<String,Object> mapping:list){
//			System.out.println(mapping.get(CodeName.AREA_CODE));
//		}


		List<Map<String, Object>> listSname = Lists.newArrayList();
		Map<String, Object> mapSname1 = new HashMap<String, Object>();
		mapSname1.put("sname","济南市");
		mapSname1.put("2014","1_0_93.22_0_");
		mapSname1.put("departid",32);
		listSname.add(mapSname1);

		Map<String, Object> mapSname2 = new HashMap<String, Object>();
		mapSname2.put("sname","东营市");
		mapSname2.put("2014","1_0_46_0_");
		mapSname2.put("departid",47);
		listSname.add(mapSname2);

		Map<String, Object> mapSname3 = new HashMap<String, Object>();
		mapSname3.put("sname","德州市");
		mapSname3.put("2014","1_0_54.3_0_");
		mapSname3.put("departid",33);
		listSname.add(mapSname3);

		List<Map<String, Object>> sortList = sortListByDepartPreset(listSname);

		for(Map<String,Object> mapping:sortList){
			System.out.println(mapping.get("sname"));
		}

	}

}